# -*- coding: utf-8 -*-
import scrapy


class AdminSpider(scrapy.Spider):
    name = 'admin'
    allowed_domains = ['youxintang.com']
    start_urls = ['http://www.youxintang.com/admin/login/']

    def parse(self, response):
        from scrapy.http import FormRequest
        p = response.xpath('//*[@id="login-form"]/input')[0]
        print(p.attrib['value'])

        request = FormRequest.from_response(response, formid='login-form',
                                            formdata={"csrfmiddlewaretoken": p.attrib['value'],
                                                      "username": 'admin',
                                                      'password': 'sonto870122'},
                                            url="http://www.youxintang.com/admin/login/",
                                            method="post",
                                            callback=self.parse_admin_data)
        yield request

    def parse_admin_data(self, response):
        print(response.text)
